<?php
namespace ImiApp\WebSocketServer\HttpController;

use Imi\Controller\HttpController;
use Imi\Server\Route\Annotation\Route;
use Imi\Server\Route\Annotation\Action;
use Imi\Server\Route\Annotation\Controller;

use Imi\Server\Server;
use Imi\Db\Db;

/**
 * @Controller("/")
 */
class IndexController extends HttpController
{
    /**
     * @Action
     * @Route("/")
     *
     * @return void
     */
    public function index()
    {
        
        // exit('11111111111');
        return [
            'data'  =>  'index',
        ];
    }

    /**
     * @Action
     *
     * @return void
     */
    public function api()
    {
        return [
            'data'  =>  'api',
        ];
    }

    /**
     * @Action
     * 
     * @Route("/sendsysmsg")
     *
     * @return string
     */
    public function sendsysmsg($content=null,$type=null)
    {
        // $server = Server::getServer(); // 获取当前服务器
        // 发送给所有 Worker

        /** @var \Imi\Server\Http\Message\Request $request */
        $request = \Imi\RequestContext::get('request');
        var_dump($request->getServerParams());

        // var_dump($request->get('token_'));

        $db = Db::query();
        $indata['send_time'] = time();
        $indata['time'] = time();
        $indata['type'] = $type;
        $indata['admin_id'] = 1;
        $indata['content'] = $content;

        // $clientInfo = $this->server->getSwooleServer()->getClientInfo($this->frame->getFd());
        $indata['ip'] = $request->getServerParams('remote_addr');
        $db->table('sc_jigou_msg')->insert($indata);

        // var_dump($indata);

        $num = Server::sendToAll([
            'type'  => $type,
            'data'  =>  $content,
            'msg'  =>  'all',
            'content'=>$content,
            'send_id'=>'1',
            'time'=>date('Y-m-d H:i:s'),
        ]);


        return $num;

      
        
        // 发送给指定 Worker
        // Server::sendMessageRaw('数据', 0);
    }



    /**
     * @Action
     * 
     * @Route("/sendsysmsgid")
     *
     * @return string
     */
    public function sendsysmsgid($id)
    {
        // $server = Server::getServer(); // 获取当前服务器
        // 发送给所有 Worker

        /** @var \Imi\Server\Http\Message\Request $request */
        $request = \Imi\RequestContext::get('request');
        var_dump($request->getServerParams());

        // var_dump($request->get('token_'));

        $db = Db::query();
        $data = $db->table('sc_jigou_msg')->where('id','=',$id)->select();
        var_dump($data);
       

        $indata['ip'] = $request->getServerParams('remote_addr');
        // $db->table('sc_jigou_msg')->insert($indata);

        // var_dump($indata);

        $num = Server::sendToAll([
            'type'  => $data['type'],
            'data'  =>  $content,
            'msg'  =>  'all',
            'content'=>$content,
            'send_id'=>'1',
            'time'=>date('Y-m-d H:i:s'),
        ]);


        return $num;

      
        
        // 发送给指定 Worker
        // Server::sendMessageRaw('数据', 0);
    }

}
